package dhee.wtqshopproject.dao;

import dhee.wtqshopproject.entity.Cart;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface CartMapper {
    // 1. 加入购物车（插入）
    @Insert("INSERT INTO cart (user_id, product_id, quantity,  create_time) " +
            "VALUES (#{userId}, #{productId}, #{quantity}, now())")
    @Options(useGeneratedKeys = true,keyColumn="cartId")
    int insertCart(Cart cart);

    // 2. 查询用户购物车全部商品
    @Select("SELECT * FROM cart WHERE user_id = #{userId}")
    List<Cart> selectCartByUserId(Integer userId);
   //根据id查询购物车
    @Select("select * from cart where cart_id=#{cartId}")
    Cart selectCartById(Integer cartId);
    // 3.1 删除单个商品
    @Delete("DELETE FROM cart WHERE cart_id=#{cartId}")
    int deleteCartItem(@Param("cartId") Integer cartId);

    // 3.2 清空购物车
    @Delete("DELETE FROM cart WHERE user_id = #{userId}")
    int clearCart(Integer userId);

    // 4. 修改商品数量
    @Update("UPDATE cart SET quantity = #{quantity} " +
            "WHERE user_id = #{userId} AND product_id = #{productId}")
    int updateQuantity(@Param("userId") Integer userId,
                       @Param("productId") Integer productId,
                       @Param("quantity") Integer quantity);

}